package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.User;
import utils.JdbcUtil;

public class UserDao {
	public User getUserByUid(int uId){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		User resultUser = null;
		try {
			con = JdbcUtil.getCon();
			resultUser = null;
			String sql = "select * from t_user where id =" + uId  ;
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()){
				resultUser=new User();
				resultUser.setId(rs.getInt("id"));
				resultUser.setUserName(rs.getString("userName"));
				resultUser.setPassword(rs.getString("password"));
				resultUser.setPhoneNumber(rs.getString("phoneNumber"));
				resultUser.setEmail(rs.getString("email"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return resultUser;
	}
	
	/**
	 * 通过phone查找用户
	 * @param phone
	 * @return
	 */
	public User getUser(String phone){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		User resultUser = null;
		try {
			con = JdbcUtil.getCon();
			resultUser = null;
			String sql = "select * from t_user where phoneNumber like '" + phone + "'";
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()){
				resultUser=new User();
				resultUser.setId(rs.getInt("id"));
				resultUser.setUserName(rs.getString("userName"));
				resultUser.setPassword(rs.getString("password"));
				resultUser.setPhoneNumber(rs.getString("phoneNumber"));
				resultUser.setEmail(rs.getString("email"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return resultUser;
	}
	public int addUser(User u){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		String sql = "insert into [t_user] (userName,password,phoneNumber,email)values(?,?,?,?)";
		
		int result = -1;
		try {
			con = JdbcUtil.getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, u.getUserName());
			pstmt.setString(2, u.getPassword());
			pstmt.setString(3, u.getPhoneNumber());
			pstmt.setString(4, u.getEmail());
			result =pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		
		return result;
	}
	public int deletUser(String id){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		String sql = "delete from t_user where id like '" + id + "'";
		
		int result = -1;
		try {
			con = JdbcUtil.getCon();
			pstmt = con.prepareStatement(sql);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return result;
	}
	
	public List<User> getUserInfo(){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		User resultUser=null;
		List<User> infoList = new ArrayList<User>();
		String sql = "select * from t_user order by userName";
		
		try {
			con = JdbcUtil.getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				resultUser=new User();
				resultUser.setId(rs.getInt("id"));
				resultUser.setUserName(rs.getString("userName"));
				resultUser.setPassword(rs.getString("password"));
				resultUser.setPhoneNumber(rs.getString("phoneNumber"));
				resultUser.setEmail(rs.getString("email"));
				infoList.add(resultUser);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return infoList;
	}
	public List<User> getserchUserInfo(String info){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		User resultUser=null;
		List<User> infoList = new ArrayList<User>();
		String sql = "select * from t_user where userName like '%"+info+"%' or phoneNumber like '%"+info+"%'";
		try {
			con = JdbcUtil.getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				resultUser=new User();
				resultUser.setId(rs.getInt("id"));
				resultUser.setUserName(rs.getString("userName"));
				resultUser.setPassword(rs.getString("password"));
				resultUser.setPhoneNumber(rs.getString("phoneNumber"));
				resultUser.setEmail(rs.getString("email"));
				infoList.add(resultUser);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return infoList;
	}
}
